package com.library.web.sys_reader.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.library.annotation.Auth;
import com.library.utils.ResultUtils;
import com.library.utils.ResultVo;
import com.library.web.sys_reader.pojo.ReaderParm;
import com.library.web.sys_reader.pojo.SysReader;
import com.library.web.sys_reader.service.SysReaderService;
import com.library.web.sys_user.pojo.SysUser;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/api/reader")
public class SysReaderController {
    @Autowired
    private SysReaderService sysReaderService;

    //读者注册
    @PostMapping("/register")
    @Auth
    public ResultVo register(@RequestBody SysReader reader) {
        //查询读者证号是否被占用
        QueryWrapper<SysReader> query = new QueryWrapper<>();
        query.lambda().eq(SysReader::getUsername, reader.getUsername());
        SysReader one = sysReaderService.getOne(query);
        if (one != null) {
            return ResultUtils.error("读者证号已存在!");
        }
        reader.setPassword(DigestUtils.md5DigestAsHex(reader.getPassword().getBytes()));
        reader.setCheckStatus("0");//未审核
        reader.setUserStatus("1");
        sysReaderService.saveReader(reader);
        return ResultUtils.success("读者注册成功!");

    }

    //新增
    @PostMapping
    @Auth
    public ResultVo addReader(@RequestBody SysReader reader) {
        //查询读者证号是否被占用
        QueryWrapper<SysReader> query = new QueryWrapper<>();
        query.lambda().eq(SysReader::getUsername, reader.getUsername());
        SysReader one = sysReaderService.getOne(query);
        if (one != null) {
            return ResultUtils.error("读者证号已存在!");
        }
        reader.setPassword(DigestUtils.md5DigestAsHex(reader.getPassword().getBytes()));
        reader.setCheckStatus("0");
        reader.setUserStatus("1");
        sysReaderService.saveReader(reader);
        return ResultUtils.success("新增读者成功!");

    }

    //编辑
    @PutMapping
    @Auth
    public ResultVo editReader(@RequestBody SysReader reader) {
        QueryWrapper<SysReader> query = new QueryWrapper<>();
        //需要用lambda的方式使用mapper ::(双冒号)java8特性
        query.lambda().eq(SysReader::getUsername, reader.getUsername());
        SysReader oneReader = sysReaderService.getOne(query);
        if (oneReader != null && !oneReader.getReaderId().equals(reader.getReaderId())) {
            return ResultUtils.error("读者证号已存在！");
        }
        sysReaderService.editReader(reader);
        return ResultUtils.success("编辑读者成功!");
    }

    //读者审核
    @PutMapping("/examineReader")
    @Auth
    public ResultVo examineReader(@RequestBody SysReader reader) {
        reader.setCheckStatus("1");
        sysReaderService.updateById(reader);
        return ResultUtils.success("审核读者成功!");
    }

    //读者是否启用
    @PutMapping("/updateStatus")
    @Auth
    public ResultVo starterReader(@RequestBody SysReader reader) {
        System.out.println(reader.getReaderId());
        QueryWrapper<SysReader> query = new QueryWrapper<>();
        //需要用lambda的方式使用mapper ::(双冒号)java8特性
        query.lambda().eq(SysReader::getReaderId, reader.getReaderId());
        SysReader oneReader = sysReaderService.getOne(query);
        if(oneReader!=null && oneReader.getUserStatus().equals("1")){ //如果是启用
            reader.setUserStatus("0");//就改成停用
            sysReaderService.updateById(reader);
            return ResultUtils.success("状态修改成功!");
        }else if(oneReader!=null && oneReader.getUserStatus().equals("0")){ //如果是停用
            reader.setUserStatus("1");//就改成启用
            sysReaderService.updateById(reader);
            return ResultUtils.success("状态修改成功!");
        }else {
            return ResultUtils.error("状态修改失败!");
        }
    }

    //删除
    @DeleteMapping("/{readerId}")
    @Auth
    public ResultVo deleteReader(@PathVariable("readerId") Long readerId) {
        boolean remove = sysReaderService.removeById(readerId);
        if (remove) {
            return ResultUtils.success("删除读者成功!");
        }
        return ResultUtils.error("删除读者失败！");
    }

    //列表
    @GetMapping("/list")
    @Auth
    public ResultVo getList(ReaderParm parm) {
        IPage<SysReader> list = sysReaderService.getList(parm);
        return ResultUtils.success("查询读者成功", list);
    }

    //根据学号查询信息
    @GetMapping("/getByUserName")
    @Auth
    public ResultVo getByUserName(SysReader reader) {
        QueryWrapper<SysReader> query = new QueryWrapper<>();
        query.lambda().eq(SysReader::getUsername, reader.getUsername());
        SysReader one = sysReaderService.getOne(query);
        if (one != null) {
            return ResultUtils.success("查询成功!", one);
        }
        return ResultUtils.success("没有该数据！");
    }

    //重置密码
    @PutMapping("/resetPassword")
    public ResultVo resetPassword(@RequestBody SysReader sysReader) {
        String passWord = "LibraryReaderUser";
        sysReader.setPassword(DigestUtils.md5DigestAsHex(passWord.getBytes()));
        boolean b = sysReaderService.updateById(sysReader);
        if (b) {
            return ResultUtils.success("重置密码成功,密码是LibraryReaderUser请及时修改密码！！！");
        }
        return ResultUtils.error("重置密码失败!");
    }
}

